package com.example.hjt_java_web_homework3.controller;

import com.example.hjt_java_web_homework3.dao.EnvironmentDataDao;
import com.example.hjt_java_web_homework3.model.EnvironmentData;
import com.example.hjt_java_web_homework3.service.EnvironmentDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Li Chun Hei
 * @date 31/10/2025
 * @description
 */
@RestController
@RequestMapping("/api/environment-data")
public class EnvironmentDataServlet extends BaseServlet {

    @Autowired
    private EnvironmentDataDao environmentDataDao;

    @GetMapping("/findAll")
    public List<EnvironmentData> findAll() {
        return environmentDataDao.findAll();
    }

    @PostMapping("/save")
    public void save(@RequestBody EnvironmentData environmentData) {
        environmentDataDao.save(environmentData);
    }

    private EnvironmentDataService environmentDataService = new EnvironmentDataService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        try {
            String pathInfo = request.getPathInfo();

            if (pathInfo == null || pathInfo.equals("/")) {
                // 获取所有环境数据
                List<EnvironmentData> dataList = environmentDataService.getAllEnvironmentData();
                sendSuccessResponse(response, dataList);
                logger.info("Retrieved all environment data, count: {}", dataList.size());

            } else if (pathInfo.equals("/location")) {
                // 根据位置获取环境数据
                String location = request.getParameter("location");
                if (location != null && !location.isEmpty()) {
                    List<EnvironmentData> dataList = environmentDataService.getEnvironmentDataByLocation(location);
                    sendSuccessResponse(response, dataList);
                    logger.info("Retrieved environment data for location: {}, count: {}", location, dataList.size());
                } else {
                    sendErrorResponse(response, HttpServletResponse.SC_BAD_REQUEST, "Location parameter is required");
                }
            }

        } catch (Exception e) {
            logger.error("Error in environment data GET request", e);
            sendErrorResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        try {
            String requestBody = getRequestBody(request);
            EnvironmentData data = objectMapper.readValue(requestBody, EnvironmentData.class);

            if (environmentDataService.recordEnvironmentData(data)) {
                sendSuccessResponse(response, data);
                logger.info("Recorded environment data for location: {}", data.getLocation());
            } else {
                sendErrorResponse(response, HttpServletResponse.SC_BAD_REQUEST, "Failed to record environment data");
            }

        } catch (Exception e) {
            logger.error("Error in environment data POST request", e);
            sendErrorResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error");
        }
    }}
